﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WHC.Hotel.BLL;
using WHC.Hotel.Entity;
using WHC.OrderWater.Commons;

namespace WHC.Hotel.UI
{
    public partial class FrmEditCaculate : BaseForm
    {
        public string ID = string.Empty;

        public FrmEditCaculate()
        {
            InitializeComponent();
        }

        private void FrmEditCaculate_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ID))
            {
                this.Text = "编辑 " + this.Text;
                TeaCaculteTypeInfo info = BLLFactory<TeaCaculteType>.Instance.FindByID(ID);
                if (info != null)
                {
                    this.txtName.Text = info.Name;
                    this.txtPrice.Text = info.Price.ToString("f2");
                }
            }
            else
            {
                this.Text = "新建 " + this.Text;
            }
            this.txtName.Focus();
        }

        private void SetInfo(TeaCaculteTypeInfo info)
        {
            info.Name = this.txtName.Text.Trim();
            info.Price = Convert.ToDecimal(this.txtPrice.Text);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.txtName.Text.Length == 0)
            {
                MessageUtil.ShowTips("请输入计费方式");
                this.txtName.Focus();
                return;
            }

            if (!string.IsNullOrEmpty(ID))
            {
                TeaCaculteTypeInfo info = BLLFactory<TeaCaculteType>.Instance.FindByID(ID);
                if (info != null)
                {
                    SetInfo(info);

                    try
                    {
                        bool succeed = BLLFactory<TeaCaculteType>.Instance.Update(info, info.ID.ToString());
                        if (succeed)
                        {
                            MessageUtil.ShowTips("保存成功");
                            this.DialogResult = DialogResult.OK;
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error(ex);
                        MessageUtil.ShowError(ex.Message);
                    }
                }
            }
            else
            {
                TeaCaculteTypeInfo info = new TeaCaculteTypeInfo();
                SetInfo(info);

                try
                {
                    bool succeed = BLLFactory<TeaCaculteType>.Instance.Insert(info);
                    if (succeed)
                    {
                        MessageUtil.ShowTips("保存成功");
                        this.DialogResult = DialogResult.OK;
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    MessageUtil.ShowError(ex.Message);
                }
            }
        }
    }
}
